﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel;
using System.Reflection;

namespace WcfContrib.Helpers
{
    /// <summary>
    /// A helper class for dealing with services and hosting
    /// </summary>
    public static class ServiceHelper
    {
        /// <summary>
        /// Adds a behavior if another of the same type does not exist.
        /// </summary>
        /// <typeparam name="TBehavior">The type of the behavior</typeparam>
        /// <typeparam name="TCreateBehavior">The type of the behavior that is created</typeparam>
        /// <param name="collection">The behaviors collection</param>
        /// <param name="behaviorInitializer">Delegate for behavior creation</param>
        public static void AddSafely<TBehavior, TCreateBehavior>(this KeyedByTypeCollection<TBehavior> collection, Func<TCreateBehavior> behaviorInitializer)
            where TCreateBehavior : TBehavior
        {
            TCreateBehavior b = collection.Find<TCreateBehavior>();

            if (b == null)
            {
                b = behaviorInitializer();

                collection.Add(b);
            }
        }
    }
}
